// MessageApp.cpp
#include "ElaWindow.h"
#include <QApplication>
#include "ElaApplication.h"
#include <QWidget>
#include <QLabel>
#include <QDebug>
#include <QListWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>


class MessagePage : public QWidget
{
  public:
    MessagePage(QWidget* parent = nullptr) : QWidget(parent)
    {
        auto layout = new QVBoxLayout(this);

        // 消息列表
        m_listWidget = new QListWidget(this);
        m_listWidget->addItem("新消息 1: 欢迎使用系统");
        m_listWidget->addItem("新消息 2: 系统更新通知");
        m_listWidget->addItem("新消息 3: 安全提醒");

        layout->addWidget(m_listWidget);

        // 操作按钮
        auto buttonLayout = new QHBoxLayout();
        auto clearBtn = new QPushButton("清空消息", this);
        auto addBtn = new QPushButton("添加测试消息", this);

        buttonLayout->addWidget(clearBtn);
        buttonLayout->addWidget(addBtn);
        layout->addLayout(buttonLayout);

        connect(clearBtn, &QPushButton::clicked, this, [this]() { m_listWidget->clear(); });

        connect(addBtn, &QPushButton::clicked, this, [this]() { m_listWidget->addItem(QString("测试消息 %1").arg(++m_messageCount)); });
    }

  private:
    QListWidget* m_listWidget;
    int m_messageCount = 0;
};

class ContactsPage : public QWidget
{
  public:
    ContactsPage(QWidget* parent = nullptr) : QWidget(parent)
    {
        auto layout = new QVBoxLayout(this);
        auto label = new QLabel("联系人列表", this);
        label->setAlignment(Qt::AlignCenter);
        layout->addWidget(label);

        // 模拟联系人列表
        auto contactsList = new QListWidget(this);
        contactsList->addItem("张三 - 在线");
        contactsList->addItem("李四 - 忙碌");
        contactsList->addItem("王五 - 离线");

        layout->addWidget(contactsList);
    }
};

class MessageAppWindow : public ElaWindow
{
  public:
    MessageAppWindow(QWidget* parent = nullptr) : ElaWindow(parent)
    {
        setupUI();
        setupConnections();
    }

  private:
    void setupUI()
    {
        setWindowTitle("消息应用");

        // 创建页面
        m_messagePage = new MessagePage();
        m_contactsPage = new ContactsPage();

        // 添加消息相关的节点
        QString messageKey;
        addExpanderNode("消息中心", messageKey, ElaIconType::Envelope);

        // 在消息中心下添加页面
        addPageNode("收件箱", m_messagePage, messageKey, 3, ElaIconType::Inbox);  // 显示3个未读

        // 添加联系人节点
        QString contactsKey;
        addExpanderNode("联系人", contactsKey, ElaIconType::Users);

        addPageNode("好友列表", m_contactsPage, contactsKey, ElaIconType::CloudSlash);

        // 底部功能
        QString settingsKey;
        addFooterNode("设置", settingsKey, 0, ElaIconType::CoinFront);

        QString exitKey;
        addFooterNode("退出", exitKey, 0, ElaIconType::PowerOff);

        // 用户信息
        setUserInfoCardTitle("张三");
        setUserInfoCardSubTitle("zhangsan@example.com");

        // 启用在新窗口打开页面
        setIsAllowPageOpenInNewWindow(true);
    }

    void setupConnections()
    {
        // 连接导航节点点击信号
        connect(this, &ElaWindow::navigationNodeClicked, this, [this](ElaNavigationType::NavigationNodeType nodeType, QString nodeKey) { qDebug() << "导航节点被点击:" << nodeType << nodeKey; });

        // 连接用户信息卡点击信号
        connect(this, &ElaWindow::userInfoCardClicked, this, []() { qDebug() << "用户信息卡被点击"; });

        // 连接关闭按钮信号
        connect(this, &ElaWindow::closeButtonClicked, this, []() { qDebug() << "关闭按钮被点击"; });
    }

  private:
    MessagePage* m_messagePage;
    ContactsPage* m_contactsPage;
};

int main(int argc, char* argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);  // 启用高 DPI (界面布局和几何)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);                                               //  启用自动高 DPI 缩放 (图标和图片)
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);  // 使用精确的缩放因子，不进行四舍五入
#else
    // 根据实际屏幕缩放比例更改
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    
    eApp->init();  // 初始化资源、字体等
    //eApp->setWindowDisplayMode(ElaApplicationType::ElaMica);

    MessageAppWindow window;
    window.resize(1200, 800);
    window.moveToCenter();
    window.show();

    return app.exec();
}